package com.example.administrator.daydayappmarket.recommend.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.recommend.adapter.RecommendDetailsAdapter;
import com.example.administrator.daydayappmarket.recommend.moduls.Details;
import com.google.gson.Gson;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RecommendMainThirdActivity extends AppCompatActivity {

    @InjectView(R.id.activity_recommend_main_third_button)
    Button buttonDownload;
    @InjectView(R.id.activity_recommend_main_third_ListView)
    ListView listView;
    @InjectView(R.id.activity_recommend_main_third_imageback)
    RelativeLayout imageback;
    @InjectView(R.id.activity_recommend_main_third_share)
    RelativeLayout share;
    @InjectView(R.id.activity_recommend_main_third_imagedownload)
    RelativeLayout download;
    @InjectView(R.id.activity_recommend_main_third_imagesearch)
    RelativeLayout search;
    @InjectView(R.id.activity_recommend_main_third_buttom)
    RelativeLayout activityRecommendMainThirdButtom;
    @InjectView(R.id.activity_recommend_main_third)
    RelativeLayout activityRecommendMainThird;
    @InjectView(R.id.activity_recommend_main_third_text_title)
    TextView title;
    @InjectView(R.id.activity_recommend_main_third_actionbar)
    RelativeLayout activityRecommendMainThirdActionbar;
    @InjectView(R.id.activity_recommend_main_third_image)
    ImageView image;
    @InjectView(R.id.activity_recommend_main_third_empty)
    RelativeLayout empty;
    private Details details;
    private RequestQueue requestQfueue;
    private StringRequest stringRequest;

    private String id;
    private String type;
    private RecommendDetailsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_recommend_main_third);
        ButterKnife.inject(this);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        type = intent.getStringExtra("type");

//        View empty = LayoutInflater.from(this).inflate(R.layout.layout_listview_empty,  listView);
        Glide.with(this).load(R.drawable.cat_loading_transparent).into(image);


        listView.setEmptyView(empty);

        requestQfueue = Volley.newRequestQueue(this);
        stringRequest = new StringRequest(Contacts.URL_SECONDRECOMMEND+type+"s/" + id, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                details = gson.fromJson(response, Details.class);
                title.setText(details.getData().getName());

                adapter = new RecommendDetailsAdapter(details, RecommendMainThirdActivity.this);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //网络请求失败


            }
        });

        stringRequest.setTag("detailsrequest");
        requestQfueue.add(stringRequest);
        requestQfueue.start();
    }

    @OnClick(R.id.activity_recommend_main_third_button)
    public void onClick() {
        //TODO 点击按钮下载apk
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQfueue.cancelAll("detailsrequest");
    }

    @OnClick({R.id.activity_recommend_main_third_imageback, R.id.activity_recommend_main_third_share, R.id.activity_recommend_main_third_imagedownload, R.id.activity_recommend_main_third_imagesearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_recommend_main_third_imageback:
                finish();
                break;
            case R.id.activity_recommend_main_third_share:

                break;
            case R.id.activity_recommend_main_third_imagedownload:
                Intent intent = new Intent(this, DownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_recommend_main_third_imagesearch:
                Intent intent1 = new Intent(this, SearchActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
